package com.eatme.web.security.authentication.password.service.impl;

import com.eatme.web.security.authentication.dao.SecurityUserDao;
import com.eatme.web.security.authentication.domain.entity.SecurityUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@Transactional
public class UserPasswordSecurityUserServiceImpl implements UserDetailsService {


    private static final Logger LOG = LoggerFactory.getLogger(UserPasswordSecurityUserServiceImpl.class);

    @Autowired
    SecurityUserDao securityUserDao;


    @Override
    public SecurityUser loadUserByUsername(String username) throws UsernameNotFoundException {
        // 1. 根据用户名去数据库查询,如果不存在抛 UsernameNotFoundException
        SecurityUser user = securityUserDao.findSecurityUserByList_Identifier(username)
                .orElseThrow(
                        () -> new UsernameNotFoundException(username+"的用户名不存在")
                );
        // 2. 比较密码 (注册时已经加密过),如果匹配成功返回UserDetails
        return user;
    }



}
